package com.example.newinfinity.Account

import android.animation.Animator
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.widget.FrameLayout.INVISIBLE
import android.widget.FrameLayout.LayoutParams
import android.widget.FrameLayout.VISIBLE
import android.widget.PopupWindow
import android.widget.Toast
import androidx.navigation.findNavController
import com.example.newinfinity.MyViewModel.UserViewModel
import com.example.newinfinity.R
import com.example.newinfinity.UserRoom.User
import com.example.newinfinity.databinding.AccountLayoutBinding
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

//关于用户中心的弹窗
class AccountPopupWindow(val context: Context, private val userViewModel: UserViewModel) {
    private var mBinding: AccountLayoutBinding? = null

    //保存数据库中当前的登录用户
    private var currentLoginInUser: User? = null

    //保存外界传入的view 为了此类中可以使用findNavController
    private var mView: View? = null

    private val accountPopupWindow: PopupWindow by lazy {
        //初始化弹窗内容，有登录用户即显示登录用户的信息界面反正显示“点击登录”界面
        mBinding = AccountLayoutBinding.inflate(LayoutInflater.from(context))
        initUi()
        PopupWindow().apply {
            this.contentView = mBinding!!.root
            this.width = LayoutParams.WRAP_CONTENT
            this.height = LayoutParams.WRAP_CONTENT
        }
    }

    private fun initUi() {
        CoroutineScope(Dispatchers.Main).launch {
            currentLoginInUser = userViewModel.getCurrentLoginInUser()

            if (currentLoginInUser != null) {
                //有登录用户
                //"点击登录"弹窗图片隐藏 显示用户登录信息弹窗
                mBinding!!.coverImageView.visibility = INVISIBLE
                //实时显示用户登录弹窗上的用户姓名
                mBinding!!.tvName.text = currentLoginInUser?.userName.toString()
                mBinding!!.tvIconName.text = currentLoginInUser?.userName?.first()?.uppercase()
            }
        }

        //点击“点击登录”弹窗时，跳转到登录界面
        mBinding!!.coverImageView.setOnClickListener {
            //此时的用户弹窗界面需要隐藏
            hide()
            mView!!.findNavController().navigate(R.id.action_homeFragment_to_loginFragment)
        }

        //配置弹窗上我的作品功能的点击实现
        mBinding!!.workLayout.setOnClickListener {
            //选中效果视图显示在这个功能上
            mBinding!!.bgIndicaterView.y = mBinding!!.workLayout.y

            //隐藏弹窗
            hide()
            //跳转到作品中心界面
            mView!!.findNavController().navigate(R.id.action_homeFragment_to_userCenterFragment)
        }

        //配置弹窗上退出登录功能的点击实现
        mBinding!!.logoffLayout.setOnClickListener {
            //选中效果视图显示在这个功能上
            mBinding!!.bgIndicaterView.y = mBinding!!.logoffLayout.y

            if (currentLoginInUser != null) {
                //退出登录
                userViewModel.cancelLoginUser()
                //更新currentLoginUser为null
                CoroutineScope(Dispatchers.Main).launch {
                    currentLoginInUser = userViewModel.getCurrentLoginInUser()
                }
                //退出用户登录前的动画 同时coverImageView显示覆盖用户信息弹窗界面
                mBinding!!.loadingView.visibility = VISIBLE
                logoffAnim()
            } else {
                Toast.makeText(context, "暂无登录用户", Toast.LENGTH_SHORT).show()
            }
        }

        ////配置弹窗上退出注销账号的点击实现
        mBinding!!.logoutLayout.setOnClickListener {
            //选中效果视图显示在这个功能上
            mBinding!!.bgIndicaterView.y = mBinding!!.logoutLayout.y
        }
    }

    //退出用户登录前的动画
    private fun logoffAnim() {
        mBinding!!.loadingView.addAnimatorListener(object : Animator.AnimatorListener {
            override fun onAnimationStart(animation: Animator) {}
            override fun onAnimationCancel(animation: Animator) {}
            override fun onAnimationRepeat(animation: Animator) {}

            override fun onAnimationEnd(animation: Animator) {
                //动画结束时 coverImageView显示覆盖用户信息弹窗界面
                mBinding!!.coverImageView.visibility = VISIBLE
                Toast.makeText(context, "退出成功！", Toast.LENGTH_SHORT).show()
            }
        })
    }

    //显示弹窗
    fun show(view: View) {
        //保存外界传入的view 为了此类中可以使用findNavController
        mView = view
        accountPopupWindow.showAsDropDown(view, 80, 20)
    }

    //隐藏弹窗
    fun hide() {
        accountPopupWindow.dismiss()
    }

}